package com.holike.crm.activity.message;

import androidx.fragment.app.Fragment;

import pony.xcode.tablayout.CommonTabLayout;
import pony.xcode.tablayout.TabEntity;
import pony.xcode.tablayout.CustomTabEntity;
import pony.xcode.tablayout.OnTabSelectListener;

import com.holike.crm.R;
import com.holike.crm.base.BaseActivity;
import com.holike.crm.bean.MessageResultBean;
import com.holike.crm.fragment.message.AbsMessageFragment;
import com.holike.crm.fragment.message.AnnouncementFragment;
import com.holike.crm.fragment.message.NewsMessageFragment;
import com.holike.crm.fragment.message.PendingMsgFragment;
import com.holike.crm.fragment.message.TreasureChestFragment;
import com.holike.crm.helper.FragmentSwitcher;
import com.holike.crm.presenter.fragment.HomePagePresenter2;
import com.holike.crm.util.Constants;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by pony on 2019/8/21.
 * Copyright holike possess 2019.
 */
class MessageHelper implements OnTabSelectListener, AbsMessageFragment.IMessageRequestCallback {

    private BaseActivity<?, ?> mActivity;
    private CommonTabLayout mTabLayout;
    private int mCurrentTab;
    private FragmentSwitcher mFragmentSwitcher;

    MessageHelper(BaseActivity<?, ?> activity) {
        this.mActivity = activity;
        mFragmentSwitcher = FragmentSwitcher.obtain(R.id.container, mActivity.getSupportFragmentManager());
        mTabLayout = mActivity.findViewById(R.id.tab_layout);
        if (mActivity.getIntent().getIntExtra(Constants.PUSH_TYPE, 0) == Constants.PUSH_TYPE_NOTIFY) { //消息推送的消息类型为待处理事项
            mCurrentTab = 1;  //切换到“待处理事项tab”
        } else {
            mCurrentTab = 0;
        }
    }

    public void setup() {
        initTab();
    }

    private void initTab() {
        List<CustomTabEntity> tabList = new ArrayList<>();
        tabList.add(new TabEntity(mActivity.getString(R.string.message_system_message))); //公告
        tabList.add(new TabEntity(mActivity.getString(R.string.message_pending_event))); //待处理
        tabList.add(new TabEntity(mActivity.getString(R.string.message_news_type))); //新闻
        tabList.add(new TabEntity(mActivity.getString(R.string.message_treasure_chest))); //百宝箱
        mTabLayout.setTabData(tabList);
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new AnnouncementFragment().setCallback(this));//公告
        fragments.add(new PendingMsgFragment().setCallback(this)); //待处理
        fragments.add(new NewsMessageFragment().setCallback(this));  //新闻
        fragments.add(new TreasureChestFragment().setCallback(this));  //百宝箱
        mTabLayout.setCurrentTab(mCurrentTab);
        mFragmentSwitcher.setup(fragments, new String[]{"pending", "system", "news", "treasure-chest"}, mCurrentTab);
        mTabLayout.setOnTabSelectListener(this);
    }

    @Override
    public void onTabSelect(int position) {
        if (position == 1) {
            MobclickAgent.onEvent(mActivity, "message_announce");
        } else {
            MobclickAgent.onEvent(mActivity, "message_notify");
        }
        mFragmentSwitcher.setCurrentTab(position);
    }

    @Override
    public void onTabReselect(int position) {

    }

    @Override
    public void onResponse(MessageResultBean bean) {
        showUnreadMessage(bean.getNoticeUnreadCount(), bean.getAnnouncementUnReadCount(), bean.getNewsUnreadCount(), bean.getDocUnreadCount());
    }

    /**
     * 显示未读消息数
     *
     * @param notifyNum   待处理事项-未读数
     * @param announceNum 公告-未读数
     * @param newsNum     新闻未读数
     * @param docNum      百宝箱未读数
     */
    private void showUnreadMessage(int notifyNum, int announceNum, int newsNum, int docNum) {
        HomePagePresenter2.setMsgNum(String.valueOf(notifyNum + announceNum + newsNum + docNum));
        if (announceNum > 0) {
            mTabLayout.showMsg(0, announceNum);
        } else {
            mTabLayout.hideMsg(0);
        }
        if (notifyNum > 0) {
            mTabLayout.showMsg(1, notifyNum);
        } else {
            mTabLayout.hideMsg(1);
        }
        if (newsNum > 0) {
            mTabLayout.showMsg(2, newsNum);
        } else {
            mTabLayout.hideMsg(2);
        }
        if (docNum > 0) {
            mTabLayout.showMsg(3, docNum);
        } else {
            mTabLayout.hideMsg(3);
        }
    }
}
